package com.whfc.ms.api.mach.controller.esgcc;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.RequestAttr;
import com.whfc.common.util.SessionAttr;
import com.whfc.common.validator.FieldScope;
import com.whfc.common.validator.FieldScopeType;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.param.MachOilAdd;
import com.whfc.mach.param.MachSetOilAddParam;
import com.whfc.ms.api.mach.service.MsMachService;
import com.whfc.ms.api.mach.service.MsMachTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author sun_guodong
 * @date 2020-11-11
 */
@RestController
@RequestMapping("/ms/api/esgcc/mach")
@Validated
public class MsEsgccMachController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachService msMachService;

    @Autowired
    private MsMachTypeService msMachTypeService;


    @PostMapping("/export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestAttribute(RequestAttr.DEPT_ID) Integer deptId) {
        logger.info("导出设备信息,loginer:{},deptId:{}", user.getUsername(), deptId);
        OssPathDTO path = msMachService.export(deptId);
        return ResultUtil.success(path);
    }

    @PostMapping("/setOilAdd")
    public Result setOilAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestBody @Validated MachSetOilAddParam param) {
        logger.info("标定设备加油量,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachService.setOilAdd(param);
        return ResultUtil.success();
    }


    @GetMapping("/getOilAdd")
    public Result getOilAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取标定设备加油量详情,loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        ListData<MachOilAdd> listData = msMachService.getOilAdd(machId, date);
        return ResultUtil.success(listData);
    }

    @GetMapping("/type/tree")
    public Result tree(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam(value = "machTypeId",  required = false) Integer machTypeId){
        logger.info("查询设备类型树。loginer:{}", user.getUsername());
        return ResultUtil.success(msMachTypeService.machLists(machTypeId));
    }

}
